package com.demo.key.show_admob

import android.graphics.Outline
import android.view.View
import android.view.ViewOutlineProvider
import android.widget.ImageView
import androidx.appcompat.widget.AppCompatImageView
import androidx.appcompat.widget.AppCompatTextView
import androidx.constraintlayout.utils.widget.ImageFilterView
import com.blankj.utilcode.util.SizeUtils
import com.demo.key.R
import com.demo.key.load_admob.AdmobKeyType
import com.demo.key.load_admob.LoadAdmob0822Manager
import com.demo.key.pa.Abs0822Page
import com.demo.key.util.show
import com.google.android.gms.ads.nativead.NativeAd
import com.google.android.gms.ads.nativead.NativeAdView
import kotlinx.coroutines.*

class ShowNativeAd0822(
    private val context: Abs0822Page,
    private val type:String
)  {
    
    private var nativeAd:NativeAd?=null
    private var loopJob:Job?=null
    
    fun loopCheckHasNativeAd(){
        LoadAdmob0822Manager.logic(type)
        loopJob= GlobalScope.launch(Dispatchers.Main) {
            delay(300L)
            while (true){
                if (!isActive) {
                    break
                }
                val admob = LoadAdmob0822Manager.getAdDataByType(type)?.admob
                if (context.resume0822&&admob!=null){
                    cancel()
                    if (admob is NativeAd){
                        nativeAd?.destroy()
                        nativeAd=admob
                        showNativeAd()
                    }
                }
                delay(1000L)
            }
        }
    }
    
    private fun showNativeAd(){
        nativeAd?.let {
            val native_ad_view=context.findViewById<NativeAdView>(R.id.native_ad_view)
            native_ad_view.mediaView=context.findViewById(R.id.media_view)
            if (null!=it.mediaContent){
                native_ad_view.mediaView?.apply {
                    setMediaContent(it.mediaContent)
                    setImageScaleType(ImageView.ScaleType.CENTER_CROP)
                    outlineProvider = provider
                }
            }
            native_ad_view.headlineView=context.findViewById(R.id.tv_native_title)
            (native_ad_view.headlineView as AppCompatTextView).text=it.headline

            native_ad_view.bodyView=context.findViewById(R.id.tv_native_desc)
            (native_ad_view.bodyView as AppCompatTextView).text=it.body

            native_ad_view.iconView=context.findViewById(R.id.iv_native_logo)
            (native_ad_view.iconView as ImageFilterView).setImageDrawable(it.icon?.drawable)

            native_ad_view.callToActionView=context.findViewById(R.id.tv_native_action)
            (native_ad_view.callToActionView as AppCompatTextView).text=it.callToAction

            native_ad_view.setNativeAd(it)
            native_ad_view.show(true)
            context.findViewById<AppCompatImageView>(R.id.iv_native_cover).show(false)

            if (AdmobKeyType.HOME==type){
                LoadAdmob0822Manager.showingFullAd0822=false
            }
            LoadAdmob0822Manager.removeAdmobData(type)
            LoadAdmob0822Manager.logic(type)
        }
    }


    private val provider=object : ViewOutlineProvider() {
        override fun getOutline(view: View?, outline: Outline?) {
            if (view == null || outline == null) return
            outline.setRoundRect(
                0,
                0,
                view.width,
                view.height,
                SizeUtils.dp2px(10F).toFloat()
            )
            view.clipToOutline = true
        }
    }

    fun onDestroy(){
        loopJob?.cancel()
        loopJob=null
    }
}